package com.ruoyi.mall.product.interfaces.facade;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mall.product.domain.service.IPmsSpuService;
import com.ruoyi.mall.product.domain.vo.SpuSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mall.product.domain.entity.PmsSpu;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * spu信息Controller
 *
 * @author zhangqishu
 * @date 2022-05-25
 */
@RestController
@RequestMapping("/spu")
public class PmsSpuController extends BaseController
{
    @Autowired
    private IPmsSpuService pmsSpuService;

    /**
     * 查询spu信息列表
     */
    @RequiresPermissions("product:spu:list")
    @GetMapping("/list")
    public TableDataInfo list(PmsSpu pmsSpu)
    {
        startPage();
        List<PmsSpu> list = pmsSpuService.selectPmsSpuList(pmsSpu);
        return getDataTable(list);
    }

    /**
     * 导出spu信息列表
     */
    @RequiresPermissions("product:spu:export")
    @Log(title = "spu信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmsSpu pmsSpu)
    {
        List<PmsSpu> list = pmsSpuService.selectPmsSpuList(pmsSpu);
        ExcelUtil<PmsSpu> util = new ExcelUtil<PmsSpu>(PmsSpu.class);
        util.exportExcel(response, list, "spu信息数据");
    }

    /**
     * 获取spu信息详细信息
     */
    @RequiresPermissions("product:spu:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pmsSpuService.selectPmsSpuById(id));
    }

    /**
     * 新增spu信息
     */
    @RequiresPermissions("product:spu:add")
    @Log(title = "spu信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsSpu pmsSpu)
    {
        return toAjax(pmsSpuService.insertPmsSpu(pmsSpu));
    }

    /**
     * TODO 保存商品
     */
    @Log(title = "spu信息", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody SpuSaveVo spuSaveVo)
    {

        return toAjax(pmsSpuService.savePmsSpuInfo(spuSaveVo));
    }

    /**
     * 修改spu信息
     */
    @RequiresPermissions("product:spu:edit")
    @Log(title = "spu信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsSpu pmsSpu)
    {
        return toAjax(pmsSpuService.updatePmsSpu(pmsSpu));
    }

    /**
     * 删除spu信息
     */
    @RequiresPermissions("product:spu:remove")
    @Log(title = "spu信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pmsSpuService.deletePmsSpuByIds(ids));
    }
}
